const mysql = require('mysql');
const fs = require('fs');
const query = require('../utils/query')

// scp -r hc_01.log  laner@10.122.251.229:/Users/laner/Desktop/log
// grep "cn.zcyun.xcloud.authcenter.controller.ControlAspect c.z.x.a.c.ControlAspect doAround START_REQUEST=url:http://10.112.73.19:2008/v1/user/regist.*password\":\"[A-Za-z]\{5\}[0-9]\{5\}" /data/logs/xcloud-authcenter/xcloud-authcenter.2019-0*-*.info.log | awk -F '"telephone":' '{print $2}' | awk -F "," '{print $1}' > /home/znuser/hc_01.log

// grep "cn.zcyun.xcloud.authcenter.controller.ControlAspect c.z.x.a.c.ControlAspect doAround START_REQUEST=url:http://10.112.73.20:2008/v1/user/regist.*password\":\"[A-Za-z]\{5\}[0-9]\{5\}" /data/logs/xcloud-authcenter/xcloud-authcenter.2019-0*-*.info.log | awk -F '"telephone":' '{print $2}' | awk -F "," '{print $1}' > /home/znuser/hc_02.log

// ls *.tar.gz | xargs -n1 tar xzvf

// 设置统计月份

let telList = uniq(fs.readFileSync(`/Users/laner/Desktop/log/hc.log`, 'utf8').toString().split("\n"));
console.log("telList", telList.lenght)
let telsGroup = group(telList, 8000)

telsGroup.forEach((tels, index) => {
    // if(index > 3) return;
    (async (tels, index) => {

        tels = tels.join(',')
        let sqlHei = "SELECT * FROM user user2 where user2.telephone in (" + tels + ")" + " and user2.uid not in (select logininfo.uid from user_login_info logininfo)"
        let rowHei = await query(sqlHei);
        rowHei = transData(rowHei);
        console.log("rowHei", rowHei.length)
        let outInsert = fs.createWriteStream(`./sql/user.insert.${index}.sql`);
        let outDelete = fs.createWriteStream(`./sql/user.delete.${index}.sql`);
        let uids = [];
        outInsert.write("INSERT INTO `user` VALUES ")

        rowHei.forEach((item, index) => {
            Object.keys(item).map(i => {
                if (isString(item[i])) {
                    item[i] = "'" + item[i] + "'"
                } else {
                    return i
                }
            })
            let {
                uid, gome_id, username, telephone, email, card_no, platform_id, apikey, birthday,
                real_name, sex, province_id, city_id, district_id, avatar_file_id, note, nickname, last_login_time,
                create_time, full_name, education, profession, height, passwd, weight, cid, mobile_company, app_id
            } = item
            uids.push(uid)
            outInsert.write(`(${uid}, ${gome_id}, ${username}, ${telephone}, ${email}, ${card_no}, ${platform_id}, ${apikey}, ${birthday}, ${real_name}, ${sex}, ${province_id}, ${city_id}, ${district_id}, ${avatar_file_id}, ${note}, ${nickname}, ${last_login_time}, ${create_time}, ${full_name}, ${education}, ${profession}, ${height},${passwd}, ${weight}, ${cid}, ${mobile_company},${app_id})`)
            if (index !== rowHei.length - 1) {
                outInsert.write(", ")
            }
        });
        let rowDevices = await query("SELECT count(*) FROM user_device where uid in (" + uids.join(',') + ");")
        console.log('rowDevices', transData(rowDevices))
        let houseDevices = await query("SELECT count(*) FROM t_house_device where uid in (" + uids.join(',') + ");")
        console.log('houseDevices', transData(houseDevices))

        outDelete.write("DELETE FROM `user` WHERE uid in (" + uids.join(',') + ");");
        outInsert.write(";")
        console.log("done")
    })(tels, index)
})

function uniq(array) {
    var temp = []; //一个新的临时数组
    for (var i = 0; i < array.length; i++) {
        if (temp.indexOf(array[i]) == -1) {
            temp.push(array[i]);
        }
    }
    return temp;
}

const transData = (data) => {
    data = JSON.stringify(data);
    data = JSON.parse(data);
    return data;
}


function isString(str) {
    return (typeof str == 'string') && str.constructor == String;
}

// getTotal();

function group(arr, num){
    num = num*1 || 1;
    var ret = [];
    arr.forEach(function(item, i){
      if(i % num === 0){
        ret.push([]);
      }
      ret[ret.length - 1].push(item);
    });
    console.log(ret);
    return ret;
  };